/*
 * @lc app=leetcode.cn id=345 lang=typescript
 *
 * [345] 反转字符串中的元音字母
 */

// @lc code=start
function reverseVowels(s: string): string {
    const result = s.split("");
    let i = 0;
    let j = s.length - 1;

    while (i < j) {
        const left = result[i];
        const right = result[j];

        const isLeft = isVowel(left);
        const isRight = isVowel(right);
        if (!isLeft) i++;
        if (!isRight) j--;
        if (isLeft && isRight) {
            result[i] = right;
            result[j] = left;
            i++;
            j--;
        }
    }
    return result.join("");
}
const cache = ["a", "e", "i", "o", "u"].reduce((col, i) => {
    return [...col, i, i.toUpperCase()];
}, [] as string[]);
const isVowel = (s: string) => cache.includes(s);
// @lc code=end
